import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

export const useAccessStore = defineStore(
  'access',
  () => {
    const accessToken = ref<string | null>(null)
    const js_code = ref<any | null>(null)
    const appid = ref<any | null>('wx93b965282dc0d5a6')
    const openid = ref<any | null>(uni.getStorageSync('openid'))
    const secret = ref<any | null>('de61daec4c1fe924ff41a4a1ab180461')

    const getAppid = () => {
      return appid.value
    }
    const getSecret = () => {
      return secret.value
    }
    const getOpenid = () => {
      return openid.value
    }

    const setAccessJs_code = (code: string) => {
      js_code.value = code
    }
    const getAccessJs_code = () => {
      return js_code.value
    }
    const removeAccessJs_code = () => {
      accessToken.value = null
    }
    /**
     * 设置访问令牌
     *
     * @param token 要设置的访问令牌
     */
    const setAccessToken = (token: string) => {
      accessToken.value = token
    }

    /**
     * 获取访问令牌
     *
     * @returns 返回当前存储的访问令牌值
     */
    const getAccessToken = () => {
      return accessToken.value
    }

    const removeAccessToken = () => {
      accessToken.value = null
    }

    /**
     * 是否登录
     */
    const isLoggedIn = computed(() => {
      // router.replace({ name: 'login' })
      return !!accessToken.value
    })

    return {
      accessToken,
      setAccessToken,
      getAccessToken,
      removeAccessToken,
      isLoggedIn,
      getAppid,
      getSecret,
      getOpenid
    }
  },
  {
    persist: true,
  },
)
